package com.mystnihon.tdroid.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.mystnihon.tdroid.R;
import com.mystnihon.tdroid.view.fragment.FtpFragment;

import com.mystnihon.tdroid.view.fragment.TorrentsListFragment;

public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {
	private static int NUM_ITEMS = 2;
	TorrentsListFragment mTorrentFragment;

	FtpFragment mFtpFragment;
	MyPagerAdapter mAdapter;
	ViewPager mPager;
	MyPageListener mMyPageSelected;
	MenuItem settings;
	MenuItem refresh;
	MenuItem upload;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

		setSupportProgressBarIndeterminateVisibility(false);
		setContentView(R.layout.main);
		initFragment(savedInstanceState);
		initSherlockActivity();

	}

	private void initSherlockActivity() {
		mAdapter = new MyPagerAdapter(getSupportFragmentManager());
		mMyPageSelected = new MyPageListener(getSupportActionBar());
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setAdapter(mAdapter);
		mPager.setOnPageChangeListener(mMyPageSelected);
		
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		String[] tabstext = getResources().getStringArray(R.array.tab_content);
		for (int i = 0; i < NUM_ITEMS; i++) {
			ActionBar.Tab tab = getSupportActionBar().newTab();
			tab.setText(tabstext[i]);
			tab.setTabListener(this);
			getSupportActionBar().addTab(tab);
		}
	}

	private void initFragment(Bundle outState) {
		if (outState != null) {

			mTorrentFragment = (TorrentsListFragment) getSupportFragmentManager().getFragment(outState,
					TorrentsListFragment.class.getSimpleName());
			mFtpFragment = (FtpFragment) getSupportFragmentManager().getFragment(outState,
					FtpFragment.class.getSimpleName());

		}

		if (mTorrentFragment == null) {
			mTorrentFragment = new TorrentsListFragment();
		}
		if (mFtpFragment == null) {
			mFtpFragment = new FtpFragment();
		}
		invalidateOptionsMenu();

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {

		getSupportFragmentManager().putFragment(outState, TorrentsListFragment.class.getSimpleName(), mTorrentFragment);
		getSupportFragmentManager().putFragment(outState, FtpFragment.class.getSimpleName(), mFtpFragment);

		super.onSaveInstanceState(outState);
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		mPager.setCurrentItem(tab.getPosition(), true);

	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		settings = menu.add(R.string.action_settings).setIcon(android.R.drawable.ic_menu_preferences)
				.setIntent(new Intent(this, SettingsActivity.class))
				.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		return super.onCreateOptionsMenu(menu);
	}

	class MyPagerAdapter extends FragmentStatePagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return NUM_ITEMS;
		}

		@Override
		public Fragment getItem(int position) {

			if (position == 0) {
				return mTorrentFragment;
			} else {
				return mFtpFragment;
			}
		}
	}


	class MyPageListener extends SimpleOnPageChangeListener {
		ActionBar bar;

		public MyPageListener(ActionBar actionbar) {
			bar = actionbar;
		}

		@Override
		public void onPageSelected(int position) {
			bar.setSelectedNavigationItem(position);

		}

	}

}

